﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Optimization;
using System.Web.Routing;
using System.Web.Security;
using SignalRPushNotification;
using Microsoft.WindowsAzure;
using Microsoft.AspNet.SignalR;
using Microsoft.ServiceBus.Messaging;
using System.Diagnostics;
using GlobalObjects;

namespace SignalRPushNotification
{
    public class Global : HttpApplication
    {

        bool IsStopped = false;
        const string TopicName = "signalr_topic_finaltopic_0";
        const string SubscriptionName = "SignalRPushNotification";
        ChatHub ch = new ChatHub();
        SubscriptionClient Client;
        void Application_Start(object sender, EventArgs e)
        {
            //// Code that runs on application startup
            //BundleConfig.RegisterBundles(BundleTable.Bundles);
            //AuthConfig.RegisterOpenAuth();
            //RouteConfig.RegisterRoutes(RouteTable.Routes);

            //GlobalHost.DependencyResolver.UseServiceBus(CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString")
            //    , 1, count, 0, "signalr");

            GlobalHost.DependencyResolver.UseServiceBus(
                CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString"), "finaltopic");
            RouteTable.Routes.MapHubs();
            Run();
        }

        void Application_End(object sender, EventArgs e)
        {
            //  Code that runs on application shutdown

        }

        void Application_Error(object sender, EventArgs e)
        {
            // Code that runs when an unhandled error occurs

        }
        public  void Run()
        {
            string connectionString = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");

            Client = SubscriptionClient.CreateFromConnectionString(connectionString, TopicName, SubscriptionName);
            //  SubscriptionClient agentSubscriptionClient = SubscriptionClient.Create(TopicName, "SignalRPushNotification");
            BrokeredMessage message = null;
            while (!IsStopped)
            {
                try
                {
                    //Receive Message from topic
                    message = Client.Receive(TimeSpan.FromSeconds(5));

                    if (message != null)
                    {
                       //Process the message
                        Debug.Print("Processing the message from the topic", message.SequenceNumber.ToString());

                        TempData data = (TempData)message.GetBody<TempData>();


                        string ex = "Hello";
                        ch.data = ex;





                        message.Complete();
                    }
                    else
                    {
                        //no more messages in the subscription
                        break;
                    }
                }

                catch (MessagingException e)
                {
                    if (!e.IsTransient)
                    {
                        Trace.WriteLine(e.Message);
                        //   throw;
                    }


                }
                catch (OperationCanceledException e)
                {
                    if (!IsStopped)
                    {
                        Trace.WriteLine(e.Message);
                        throw;
                    }
                }
            }
        }

    }
}
